package com.mapull.example.section1;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author Mapull
 * @date 2024-03-19
 */
public class StringList {

    public static void main(String[] args) {
        String text = "Hello Java";
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }
    }
    public static void main4(String[] args) {
        String text = "Java string";
        char[] res = new char[4];
        text.getChars(0,4,res, 2);
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }
    }


    public static void main2(String[] args) {
        String str = "Java";
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
    }

    public static void main3(String[] args) {
        String str = "Java";
        try {
            System.out.println("----UTF-8----");
            byte[] bytes = str.getBytes("UTF-8");
            for (int i = 0; i < bytes.length; i++) {
                System.out.println(bytes[i]);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        byte[] bytes1 = str.getBytes(Charset.defaultCharset());
        byte[] bytes2 = str.getBytes(StandardCharsets.UTF_8);
        System.out.println("----GBK----");
        byte[] bytes = str.getBytes(Charset.forName("GBK"));
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
    }
}
